package question1;

import java.io.*;

public class WriteAndRead {
    //先创建目录mdir
    public static void mDir(String dirPath){
        File objDir = new File(dirPath);
        if(objDir.mkdirs()){
            System.out.println("创建成功！");
        }
        else {
            System.out.println("创建失败！");
        }
    }
    //在文件夹下创建文件
    public static void mFile(String dirName,String fileName){
    File f = new File(dirName,fileName);
    if(f.exists()){
        System.out.println("文件已存在无法创建！");
        return;
    }
    else {
        System.out.println("创建成功！");
    }
    }
    //在指定目录里创建文件
    public static boolean creatFile(String destFileName) {
        File file = new File(destFileName);
        if (file.exists()) {
            System.out.println("创建单个文件" + destFileName + "失败，目标文件已存在！");
            return false;
        }
        if (destFileName.endsWith(File.separator)) {
            System.out.println("创建单个文件" + destFileName + "失败，目标文件不能为目录！");
            return false;
        }
        //判断目标文件所在的目录是否存在
        if (!file.getParentFile().exists()) {
            System.out.println("目标文件所在目录不存在，正在创建");
        }
        if (!file.getParentFile().mkdirs()) {
            System.out.println("创建失败！");
            return false;
        }
        //创建目标文件
        try {
            if (file.createNewFile()) {
                System.out.println("创建单个文件" + destFileName + "成功！");
                return true;
            } else {
                System.out.println("创建单个文件" + destFileName + "失败！");
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建单个文件" + destFileName + "失败！");
            return false;
        }
        return false;
    }
    public static void write(String word,String path){
        FileOutputStream fs = null;
        try{
            fs = new FileOutputStream(path);

            byte[] date = word.getBytes("UTF-8");
            fs.write(date);
            fs.flush();
            System.out.println("保存文件成功！");
        }catch (IOException e){
            e.printStackTrace();

        }
        finally {
            if(fs != null){
                try {
                    fs.close();
                }catch (IOException ex){
                    ex.printStackTrace();
                }
            }
        }
    }
    //读文件FileInputStream
    public static void read(String path) throws  IOException{

        FileInputStream fi = null;
        fi = new FileInputStream(path);
        byte[] buffer = new byte[1024];
        int len = -1;
        String str = null;
        while ((len = fi.read(buffer)) != -1){
            str = new String(buffer,0,len);
            System.out.println(str+"\n一共有"+len+"个字节。");
        }

    }
    public static void main(String[] args) throws IOException{
        creatFile("E:\\myPic\\helloWord.txt");
        write("假如生活欺骗了你不要悲伤不要着急，忧郁的日子里需要镇静相信吧快乐的日子将会来临！","E:\\myPi\\helloWord.txt");
        read("E:\\myPi\\helloWord.txt");
    }

}
